package com.longnuoqi.mydoctor.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.longnuoqi.mydoctor.activity.UserFavoriteActivity;
import com.longnuoqi.mydoctor.activity.UserLandActivity;
import com.longnuoqi.mydoctor.activity.UserProfileActivity;
import com.yunhe.mydoctor.R;


/**
 * Created by Lenovo on 2015/12/28.
 */
public class UserFragment extends Fragment {
    private View view;
    private SharedPreferences sharedPreferences;
    private boolean isLogin = false;
    private String accountName;
    private TextView account;
    private RelativeLayout question;
    private ImageView queImg;
    private RelativeLayout favorite;
    private ImageView favorImg;
    private RelativeLayout profile;
    private ImageView proImg;
    private RelativeLayout message;
    private ImageView msgImg;
    private RelativeLayout suggestion;
    private ImageView sugImg;
    private RelativeLayout clear;
    private ImageView clearImg;
    private RelativeLayout attention;
    private ImageView attImg;
    private RelativeLayout out;
    private ImageView outImg;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.user_layout, container, false);

        sharedPreferences = getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        isLogin = sharedPreferences.getBoolean("islogin", false);
        init();
        initView();
        return view;
    }

    private void initView() {
        if (isLogin) {
            accountName = sharedPreferences.getString("account", "");
            account.setText("用户：" + accountName);
            account.setTextColor(Color.BLACK);
            account.setTextSize(14);
            account.setBackground(null);
            queImg.setImageResource(R.mipmap.user_question_land);
            favorImg.setImageResource(R.mipmap.user_favorite_land);
            proImg.setImageResource(R.mipmap.user_profile_land);
            msgImg.setImageResource(R.mipmap.user_message_land);
            sugImg.setImageResource(R.mipmap.user_suggestion_land);
            clearImg.setImageResource(R.mipmap.user_clear_land);
            attImg.setImageResource(R.mipmap.user_attention_land);
            outImg.setImageResource(R.mipmap.user_more_land);
        } else {
            account.setText("登录/注册");
            account.setTextColor(Color.WHITE);
            account.setBackgroundResource(R.mipmap.user_btn);
            queImg.setImageResource(R.mipmap.user_question);
            favorImg.setImageResource(R.mipmap.user_favorite);
            proImg.setImageResource(R.mipmap.user_profile);
            msgImg.setImageResource(R.mipmap.user_message);
            sugImg.setImageResource(R.mipmap.user_suggestion);
            clearImg.setImageResource(R.mipmap.user_clear);
            attImg.setImageResource(R.mipmap.user_attention);
            outImg.setImageResource(R.mipmap.user_more);
        }
    }


    private void init() {
        account = (TextView) view.findViewById(R.id.user_account);

        question = (RelativeLayout) view.findViewById(R.id.user_question);
        queImg = (ImageView) view.findViewById(R.id.user_question_img);
        favorite = (RelativeLayout) view.findViewById(R.id.user_favorite);
        favorImg = (ImageView) view.findViewById(R.id.user_favorite_img);
        profile = (RelativeLayout) view.findViewById(R.id.user_profile);
        proImg = (ImageView) view.findViewById(R.id.user_profile_img);
        message = (RelativeLayout) view.findViewById(R.id.user_message);
        msgImg = (ImageView) view.findViewById(R.id.user_message_img);
        suggestion = (RelativeLayout) view.findViewById(R.id.user_suggestion);
        sugImg = (ImageView) view.findViewById(R.id.user_suggestion_img);
        clear = (RelativeLayout) view.findViewById(R.id.user_clear);
        clearImg = (ImageView) view.findViewById(R.id.user_clear_img);
        attention = (RelativeLayout) view.findViewById(R.id.user_attention);
        attImg = (ImageView) view.findViewById(R.id.user_attention_img);
        out = (RelativeLayout) view.findViewById(R.id.user_out);
        outImg = (ImageView) view.findViewById(R.id.user_out_img);

        account.setOnClickListener(onClickListener);
        question.setOnClickListener(onClickListener);
        favorite.setOnClickListener(onClickListener);
        profile.setOnClickListener(onClickListener);
        message.setOnClickListener(onClickListener);
        suggestion.setOnClickListener(onClickListener);
        clear.setOnClickListener(onClickListener);
        attention.setOnClickListener(onClickListener);
        out.setOnClickListener(onClickListener);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.user_account:
                    Intent intent = new Intent(getActivity(), UserLandActivity.class);
                    startActivityForResult(intent, 0);
                    break;
                case R.id.user_question:
                    if (isLogin) {
                        Toast.makeText(getActivity(), "我的提问", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_favorite:
                    if (isLogin) {
                        Intent favorite = new Intent(getActivity(), UserFavoriteActivity.class);
                        startActivityForResult(favorite, 0);
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_profile:
                    if (isLogin) {
                        Intent profile = new Intent(getActivity(), UserProfileActivity.class);
                        startActivityForResult(profile, 0);
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_message:
                    if (isLogin) {
                        Toast.makeText(getActivity(), "user_message", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_suggestion:
                    if (isLogin) {
                        Toast.makeText(getActivity(), "user_suggestion", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_clear:
                    if (isLogin) {
                        Toast.makeText(getActivity(), "user_clear", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_attention:
                    if (isLogin) {
                        Toast.makeText(getActivity(), "user_attention", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.user_out:
                    if (isLogin) {
                        showAlertDialog(getActivity(), "是否确定退出登录？");
                    } else {
                        Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    private void showAlertDialog(Context context, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("提示：");
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                clearSharedPreferences();
                dialog.dismiss();
                isLogin = sharedPreferences.getBoolean("islogin", false);
                initView();

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public void clearSharedPreferences() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("access_token", "");
        editor.putString("refresh_token", "");
        editor.putString("account", "");
        editor.putString("password", "");
        editor.putBoolean("islogin", false);
        editor.putInt("id", -1);
        editor.commit();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 1:
                isLogin = sharedPreferences.getBoolean("islogin", false);
                initView();
                Toast.makeText(getActivity(), "欢迎回来！", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
